var mongodb = require('./db'),
	markdown = require('markdown').markdown;

function Post(name, title, tags, content){
	this.name = name;
	this.title = title;
	this.tags = tags;
	this.content = content;
}

module.exports = Post;

Post.prototype.save = function(callback){
	var date = new Date();
	
	var time = {
		date : date,
		year : date.getFullYear(),
		month : date.getFullYear() + "-" + (date.getMonth()+1),
		day : date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate(),
		minute : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() + ":" + (date.getMinutes()<10 ? '0'+date.getMinutes : date.getMinutes())
	}
	
	var post = {
		name : this.name,
		time : time,
		title : this.title,
		tags : this.tags,
		content : this.content
	};
	
	mongodb.open(function(err,db){
		if(err){
			return callback(err);
		}
		
		db.collection('posts', function(err, collection){
			if(err){
				mongodb.close();
				return callback(err);
			}
			
			collection.insert(post,{
				safe:true
			},function(err){
				mongodb.close();
				if(err){
					return callback(err);
				}
				
				callback(null);
			});
			
		});
	});
	
	

};


Post.getAll = function(name , callback){
	
	mongodb.open(function (err,db) {
		if(err){
			return callback(err);
		}
		
		db.collection('posts',function(err, collection){
		
		if(err){
			mongodb.close();
			return callback(err);
		}
		
		var query = {};
		
		if(name){
			query.name = name;
		}
		
		collection.find(query).sort({
			time: -1
		}).toArray(function(err ,docs){
			mongodb.close();
			if(err){
				return callback(err);
			}
			/*
				docs.forEach(function(doc){
					doc.content = markdown.toHTML(doc.content);
				});
				*/
			callback(null,docs);
			});
	
		});
	});

};

Post.getOne = function(name, day, title, callback) {
  mongodb.open(function (err, db) {
    if (err) {
      return callback(err);
    }
    db.collection('posts', function (err, collection) {
      if (err) {
        mongodb.close();
        return callback(err);
      }
      collection.findOne({
        "name": name,
        "time.day": day,
        "title": title
      }, function (err, doc) {
        mongodb.close();
        if (err) {
          return callback(err);
        }
        doc.content = markdown.toHTML(doc.content);
        callback(null, doc);
      });
    });
  });
};









